/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package ru.apertum.diasled.forms;

import com.jogamp.newt.event.KeyEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.TableCellRenderer;
import ru.apertum.diasled.redactor.CfgCoefsTableModel;
import ru.apertum.diasled.cfg.Configuration;

/**
 *
 * @author Evgeniy Egorov
 */
public class FRedactorParams extends javax.swing.JDialog {

    public static FRedactorParams getInstance() {
        FRedactorParamsHolder.INSTANCE.result = false;
        return FRedactorParamsHolder.INSTANCE;
    }

    private static class FRedactorParamsHolder {

        private static final FRedactorParams INSTANCE = new FRedactorParams(null, true);
    }

    public void loadConfig(Configuration config) {
        textFieldSysName.setText(config.getSystemName());
        spinnerCoeffRecountWeigth.setValue(config.getWeightConversionCoeff());
        ((CfgCoefsTableModel) (tableCoefs.getModel())).setCoefs(config.getCoefs());
        ((CfgCoefsTableModel) (tableCoefs.getModel())).fireTableDataChanged();
    }

    public void saveConfig(Configuration config) {
        config.setSystemName(textFieldSysName.getText());
        config.setWeightConversionCoeff((float) spinnerCoeffRecountWeigth.getValue());
        config.setCoefs(((CfgCoefsTableModel) (tableCoefs.getModel())).getCoefs());
    }

    /** Creates new form FRedactorParams */
    public FRedactorParams(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try {
            setIconImage(ImageIO.read(DiasledView.class.getResource("/ru/apertum/diasled/forms/resources/redactor.png")));
        } catch (IOException ex) {
            System.err.println(ex);
        }

        // свернем по esc
        getRootPane().registerKeyboardAction(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                result = false;
                setVisible(false);
            }
        },
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);

        tableCoefs.setModel(new CfgCoefsTableModel(null));
        tableCoefs.setDefaultRenderer(Integer.class, new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                final JLabel l = new JLabel("" + value);
                l.setFont(new Font("Arial", 2, 12));
                l.setOpaque(true);
                l.setBackground(CLR);
                l.setHorizontalAlignment(JLabel.CENTER);
                return l;
            }
        });
    }
    private static final Color CLR = new Color(240, 240, 240);

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        spinnerCoeffRecountWeigth = new javax.swing.JSpinner();
        textFieldSysName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        buttonCancel = new javax.swing.JButton();
        buttonOK = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCoefs = new javax.swing.JTable();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ru.apertum.diasled.Diasled.class).getContext().getResourceMap(FRedactorParams.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        spinnerCoeffRecountWeigth.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(1.0f), Float.valueOf(10.0f), Float.valueOf(0.05f)));
        spinnerCoeffRecountWeigth.setName("spinnerCoeffRecountWeigth"); // NOI18N

        textFieldSysName.setText(resourceMap.getString("textFieldSysName.text")); // NOI18N
        textFieldSysName.setName("textFieldSysName"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        buttonCancel.setText(resourceMap.getString("buttonCancel.text")); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonOK.setText(resourceMap.getString("buttonOK.text")); // NOI18N
        buttonOK.setName("buttonOK"); // NOI18N
        buttonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOKActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableCoefs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10", "Title 11", "Title 12", "Title 13", "Title 14", "Title 15", "Title 16", "Title 17", "Title 18", "Title 19", "Title 20", "Title 21", "Title 22", "Title 23", "Title 24", "Title 25", "Title 26", "Title 27", "Title 28", "Title 29", "Title 30", "Title 31", "Title 32"
            }
        ));
        tableCoefs.setCellSelectionEnabled(true);
        tableCoefs.setName("tableCoefs"); // NOI18N
        tableCoefs.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tableCoefs);
        tableCoefs.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title0")); // NOI18N
        tableCoefs.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title1")); // NOI18N
        tableCoefs.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title2")); // NOI18N
        tableCoefs.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title3")); // NOI18N
        tableCoefs.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title4")); // NOI18N
        tableCoefs.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title5")); // NOI18N
        tableCoefs.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title6")); // NOI18N
        tableCoefs.getColumnModel().getColumn(7).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title7")); // NOI18N
        tableCoefs.getColumnModel().getColumn(8).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title8")); // NOI18N
        tableCoefs.getColumnModel().getColumn(9).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title9")); // NOI18N
        tableCoefs.getColumnModel().getColumn(10).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title10")); // NOI18N
        tableCoefs.getColumnModel().getColumn(11).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title11")); // NOI18N
        tableCoefs.getColumnModel().getColumn(12).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title12")); // NOI18N
        tableCoefs.getColumnModel().getColumn(13).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title13")); // NOI18N
        tableCoefs.getColumnModel().getColumn(14).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title14")); // NOI18N
        tableCoefs.getColumnModel().getColumn(15).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title15")); // NOI18N
        tableCoefs.getColumnModel().getColumn(16).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title16")); // NOI18N
        tableCoefs.getColumnModel().getColumn(17).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title17")); // NOI18N
        tableCoefs.getColumnModel().getColumn(18).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title18")); // NOI18N
        tableCoefs.getColumnModel().getColumn(19).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title19")); // NOI18N
        tableCoefs.getColumnModel().getColumn(20).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title20")); // NOI18N
        tableCoefs.getColumnModel().getColumn(21).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title21")); // NOI18N
        tableCoefs.getColumnModel().getColumn(22).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title22")); // NOI18N
        tableCoefs.getColumnModel().getColumn(23).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title23")); // NOI18N
        tableCoefs.getColumnModel().getColumn(24).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title24")); // NOI18N
        tableCoefs.getColumnModel().getColumn(25).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title25")); // NOI18N
        tableCoefs.getColumnModel().getColumn(26).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title26")); // NOI18N
        tableCoefs.getColumnModel().getColumn(27).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title27")); // NOI18N
        tableCoefs.getColumnModel().getColumn(28).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title28")); // NOI18N
        tableCoefs.getColumnModel().getColumn(29).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title29")); // NOI18N
        tableCoefs.getColumnModel().getColumn(30).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title30")); // NOI18N
        tableCoefs.getColumnModel().getColumn(31).setHeaderValue(resourceMap.getString("tableCoefs.columnModel.title31")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 923, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(textFieldSysName, javax.swing.GroupLayout.DEFAULT_SIZE, 795, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonOK)
                        .addGap(18, 18, 18)
                        .addComponent(buttonCancel))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(48, 48, 48)
                        .addComponent(spinnerCoeffRecountWeigth, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldSysName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(spinnerCoeffRecountWeigth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonOK))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private boolean result = false;

    public boolean isOK() {
        return result;
    }

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        result = false;
        setVisible(false);
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed
        result = true;
        setVisible(false);
    }//GEN-LAST:event_buttonOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spinnerCoeffRecountWeigth;
    private javax.swing.JTable tableCoefs;
    private javax.swing.JTextField textFieldSysName;
    // End of variables declaration//GEN-END:variables
}
